package com.calclab.hablar.chatmedia.client.preferences;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class ChatMediaPreferenceWidget extends Composite implements ChatMediaPreferencesDisplay {

    private static ChatMediaPreferenceWidgetUiBinder uiBinder = GWT.create(ChatMediaPreferenceWidgetUiBinder.class);

    interface ChatMediaPreferenceWidgetUiBinder extends UiBinder<Widget, ChatMediaPreferenceWidget> {
    }

    @UiField
    SpanElement icon;
    @UiField
    Label loadingMessage;
//    @UiField
//    FlexTable header;
    @UiField
    FlexTable table;

    @UiField 
    FlowPanel addMediaPanel;
    
    @UiField
    DivElement loading;
    
    @UiField Anchor addMedia;
    
    private int startIndex, selectedRow = -1;

    
    public ChatMediaPreferenceWidget() {
	initWidget(uiBinder.createAndBindUi(this));
	initTable();
    }

    @UiHandler("table")
    void onTableClicked(ClickEvent event) {
	// Select the row that was clicked (-1 to account for header row).
	Cell cell = table.getCellForEvent(event);
	if (cell != null) {
	    int row = cell.getRowIndex();
	    selectRow(row);
	}
    }

    private void selectRow(int row) {
	// When a row (other than the first one, which is used as a header) is
	// selected, display its associated MailItem.
    }

    private void initTable() {
	// Initialize the header.
//	header.getColumnFormatter().setWidth(0, "128px");
//	header.getColumnFormatter().setWidth(1, "192px");
//	header.getColumnFormatter().setWidth(2, "192px");

//	header.setText(0, 0, "Default");
//	header.setText(0, 1, "Type");
//	header.setText(0, 2, "Details");
//	header.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);

	table.getColumnFormatter().setWidth(0, "128px");
	table.getColumnFormatter().setWidth(1, "192px");
	table.getColumnFormatter().setWidth(2, "192px");
	
	// Initialize the table.
	table.setText(0, 0, "Default");
	table.setText(0, 1, "Type");
	table.setText(0, 2, "Details");
	
	addRow("web", "best001");
	addRow("im","best001@imdev.com");
    }

    private void addRow(String type, String account) {
	int rowCount = table.getRowCount();
	table.setWidget(rowCount, 0, new CheckBox());
	table.setText(rowCount, 1, type);
	table.setText(rowCount, 2, account);
    }

    @Override
    public HasText getLoading() {
	return loadingMessage;
    }

    @Override
    public void setFormVisible(final boolean visible) {
//	header.setVisible(visible);
	table.setVisible(visible);
    }

    @Override
    public void setLoadingVisible(boolean visible) {
	setVisible(loading, visible);
    }

    @Override
    public Widget asWidget() {
	return this;
    }
    

    @Override
    public HasClickHandlers getAdd() {
	// TODO Auto-generated method stub
	return addMedia;
    }
    
    public void add(Widget element){
	if(addMediaPanel.getWidgetCount() > 0){
//	    addMedia.setText("Add im/email/web account");
	    addMediaPanel.remove(0);
	}else{
//	    addMedia.setText("cancel add account");
	    addMediaPanel.add(element);
	}
    }

    public void remove(Widget element){
	addMediaPanel.remove(element);
    }
    
    
}
